<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="文章标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入文章标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" placeholder="请输入作者" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="分类" prop="categoryId">
            <el-select v-model="form.categoryId" placeholder="请选择分类" style="width: 100%;">
              <el-option label="请选择" value="" />
              <!-- 这里需要从后端获取分类列表 -->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标签" prop="tags">
            <el-select v-model="form.tags" multiple placeholder="请选择标签" style="width: 100%;">
              <el-option label="请选择" value="" />
              <!-- 这里需要从后端获取标签列表 -->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="副标题" prop="subtitle">
            <el-input v-model="form.subtitle" placeholder="请输入副标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="来源" prop="source">
            <el-input v-model="form.source" placeholder="请输入来源" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="摘要" prop="summary">
        <el-input v-model="form.summary" type="textarea" placeholder="请输入摘要" />
      </el-form-item>
      
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="form.keywords" placeholder="请输入关键词，多个关键词用逗号分隔" />
      </el-form-item>
      
      <el-form-item label="封面图片" prop="coverImage">
        <el-upload
          class="avatar-uploader"
          action="#"
          :http-request="handleUpload"
          :show-file-list="false"
        >
          <img v-if="form.coverImage" :src="form.coverImage" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      
      <el-form-item label="文章内容" prop="content">
        <editor v-model="form.content" :min-height="192" />
      </el-form-item>
      
      <el-row>
        <el-col :span="8">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="0">草稿</el-radio>
              <el-radio label="1">发布</el-radio>
              <el-radio label="2">撤回</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否置顶" prop="isTop">
            <el-radio-group v-model="form.isTop">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否热门" prop="isHot">
            <el-radio-group v-model="form.isHot">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          v-model="form.publishTime"
          type="datetime"
          placeholder="选择发布时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%;"
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addArticle } from "@/api/cms/article";
import Editor from '@/components/Editor';

export default {
  name: "ArticleCreate",
  components: {
    Editor
  },
  data() {
    return {
      // 表单参数
      form: {
        categoryId: null,
        title: null,
        subtitle: null,
        summary: null,
        content: null,
        author: null,
        coverImage: null,
        keywords: null,
        source: null,
        status: "0",
        isTop: "0",
        isHot: "0",
        viewCount: 0,
        likeCount: 0,
        commentCount: 0,
        publishTime: null,
        remark: null,
        tags: []
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        author: [
          { required: true, message: "作者不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],
        content: [
          { required: true, message: "文章内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 文件上传
    handleUpload(file) {
      // 模拟上传操作，实际应调用后端接口
      const reader = new FileReader();
      reader.onload = (e) => {
        this.form.coverImage = e.target.result;
      };
      reader.readAsDataURL(file.file);
    },
    // 取消按钮
    cancel() {
      this.$router.push('/cms/article');
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId: null,
        title: null,
        subtitle: null,
        summary: null,
        content: null,
        author: null,
        coverImage: null,
        keywords: null,
        source: null,
        status: "0",
        isTop: "0",
        isHot: "0",
        viewCount: 0,
        likeCount: 0,
        commentCount: 0,
        publishTime: null,
        remark: null,
        tags: []
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addArticle(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.$router.push('/cms/article');
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>